//
//  ZCXVideoViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXVideoViewController.h"
#import "ZCXVideoCollectionViewCell.h"
#import "ZFHYNetWork.h"
#import "ZCXVideoModel.h"
#import "ZCXVideoDetialViewController.h"
#import "ZCXNewPetVideoDetialViewController.h"
#import "ZCXMediaModel.h"
#import <MJRefresh.h>
#import "FileManage.h"
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXVideoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *itemsArray;
@property (nonatomic, retain) MJRefreshBackGifFooter *footer;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, copy) NSString *path;
@property (nonatomic, assign) BOOL fun;
@end

@implementation ZCXVideoViewController
- (void)dealloc{
    [_collectionView release];
    [_itemsArray release];
    [_footer release];
    [_path release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
     self.view.backgroundColor = [UIColor whiteColor];
     self.itemsArray = [NSMutableArray array];
     self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"萌宠乐园";
    self.view.backgroundColor = [UIColor blackColor];
    _pageNumber = 1;
    [self modelRequest:@"http://newapi.meipai.com/channels/feed_timeline.json?id=6&type=2&feature=hot&page=1&device_id=860923029360494&version=4100"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //注册Cell
    [_collectionView registerClass:[ZCXVideoCollectionViewCell class] forCellWithReuseIdentifier:@"videoCollectionViewcell"];
    [self.view addSubview:_collectionView];
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _fun = NO;
        [self modelRequest:@"http://newapi.meipai.com/channels/feed_timeline.json?id=6&type=2&feature=hot&page=1&device_id=860923029360494&version=4100"];
    }];
    [self Footer];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark 数据请求
-(void)modelRequest:(NSString *)urlString{
    [ZFHYNetWork handleGETWithUrlString:urlString parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSArray *array = responseObject;
        if (_fun == NO) {
            [self.itemsArray removeAllObjects];
        }
        if (array.count == 0) {
            [self.collectionView.footer endRefreshingWithNoMoreData];
        }else{
            
        for (NSDictionary *dic in array) {
            if ([[dic objectForKey:@"type"] isEqualToString:@"media"]) {
                ZCXVideoModel *videoModel = [ZCXVideoModel ZCXVideoModelWithDictionary:dic];
                [self.itemsArray addObject:videoModel];
            }
        }
        [self.collectionView.footer endRefreshing];
        }
        [self.collectionView.header endRefreshing];
        [self.collectionView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"");
    }];
}
#pragma mark 上拉加载
-(void)Footer{
    self.footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    _collectionView.footer = self.footer;
}
- (void)footerRefresh{
     _fun = YES;
    _pageNumber += 1;
    [self modelRequest:[NSString stringWithFormat:@"http://newapi.meipai.com/channels/feed_timeline.json?id=6&type=2&feature=hot&page=%ld&device_id=860923029360494&version=4100", _pageNumber]];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZCXVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"videoCollectionViewcell" forIndexPath:indexPath];
    cell.videoModel = self.itemsArray[indexPath.row];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemsArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width / 2, 200 * HEIGHT);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row == 0) {
//        ZCXNewPetVideoDetialViewController *newVideoDetial = [[ZCXNewPetVideoDetialViewController alloc]init];
//        [self.navigationController pushViewController:newVideoDetial animated:YES];
//        [newVideoDetial release];
//    }else{
    ZCXVideoDetialViewController *videoDetial = [[ZCXVideoDetialViewController alloc]init];
        NSDictionary *dic = [self.itemsArray[indexPath.row] media];
        ZCXMediaModel *mediaModel = [ZCXMediaModel ZCXMedioModelWithDictionary:dic];
        videoDetial.ID = mediaModel.ID;
        self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:videoDetial animated:YES];
    [videoDetial release];
//    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
